package com.kj.tms.common.bean.exception;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.text.MessageFormat;

@ControllerAdvice
public class ExceptionHandlerController {
    private static Logger logger = LoggerFactory.getLogger(ExceptionHandlerController.class);

    @ExceptionHandler(RuntimeException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public JSONObject runtimeExceptionHandler(RuntimeException ex) {
        logger.error("runtimeExceptionHandler, msg:{}, exception:{}", ex.toString(), ex);
        JSONObject response = new JSONObject();
        response.put("message", "Internal Server Error");
        response.put("code","00003");
        return response;
    }

    @ExceptionHandler(NullPointerException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public JSONObject nullPointerExceptionHandler(NullPointerException ex) {
        logger.error("nullPointerExceptionHandler, msg:{}, exception:{}", ex.toString(), ex);
        JSONObject response = new JSONObject();
        response.put("message", "Internal Server Error");
        return response;
    }

    /*----- REQUEST ERROR -----*/
    @ExceptionHandler({ BusinessException.class })
    @ResponseStatus(HttpStatus.FORBIDDEN)
    @ResponseBody
    public JSONObject requestForbidden(BusinessException ex) {
        logger.error("ForbiddenExceptionHandler, msg:{}, exception:{}", ex.toString(), ex);
        JSONObject response = new JSONObject();
        response.put("message", ex.getMessage());
        response.put("code", ex.getCode());
        return response;
    }

    @ExceptionHandler({ ServiceException.class })
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ResponseBody
    public JSONObject requestTypeMismatch(ServiceException ex) {
        logger.error("ServiceException, msg:{}, exception:{}", ex.toString(), ex);
        JSONObject response = new JSONObject();
        response.put("message", "Not service");
        response.put("code","00002");
        return response;
    }

    @ExceptionHandler({ MissingServletRequestParameterException.class })
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ResponseBody
    public JSONObject requestMissingServletRequest(MissingServletRequestParameterException ex) {
        logger.error("MissingServletRequestParameterExceptionHandler, msg:{}, exception:{}", ex.toString(), ex);
        JSONObject response = new JSONObject();
        // response.put("message", "Bad Request");
        String strTemplate = "Bad Request, param:{0} is required, type:{1}";
        response.put("message", MessageFormat.format(strTemplate, ex.getParameterName(), ex.getParameterType()));
        return response;
    }

    @ExceptionHandler({ SysException.class })
    @ResponseStatus(HttpStatus.TEMPORARY_REDIRECT)
    @ResponseBody
    public JSONObject SysExceptionHandler(SysException ex) {
        logger.error("SysExceptionHandler, msg:{}, exception:{}", ex.toString(), ex);
        JSONObject response = new JSONObject();
        response.put("message", "system error");
        response.put("code","00001");
        return response;
    }

    /*----- REQUEST ERROR -----*/
    @ExceptionHandler({ HttpMessageNotReadableException.class })
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ResponseBody
    public JSONObject requestNotReadable(HttpMessageNotReadableException ex) {
        logger.error("HttpMessageNotReadableExceptionHandler, msg:{}, exception:{}", ex.toString(), ex);
        JSONObject response = new JSONObject();
        response.put("message", "Bad Request");
        return response;
    }

    @ExceptionHandler({ HttpRequestMethodNotSupportedException.class })
    @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
    @ResponseBody
    public JSONObject request405(HttpRequestMethodNotSupportedException ex) {
        logger.error("HttpRequestMethodNotSupportedExceptionHandler, msg:{}, exception:{}", ex.toString(), ex);
        JSONObject response = new JSONObject();
        // response.put("message", "Method Not Allowed");
        response.put("message", ex.getMessage());
        return response;
    }
}
